package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.ImageIcon;

import network.Netevent;
import network.Netlistener;
import templates.Tower;
import templates.TowerTemplate;
import etc.Coord;
import etc.Parser;

public class Guimap extends Component implements Netlistener {

	private ImageIcon[][] map;
	public static int TILE_WIDTH=30,TILE_HEIGHT=30;
	public static int MAP_WIDTH=40, MAP_HEIGHT=20;
//	private Set<Enemy> enemies;
	private Set<Tower> towers = new HashSet<Tower>();
	private Map<String, TowerTemplate> towertemps = new HashMap<String, TowerTemplate>();
	
	public Guimap() {
		Set<TowerTemplate> s = Parser.getTowers();
		for (TowerTemplate t: s) {
			towertemps.put(t.name, t);
		}
		
		setMap();
		
		this.setPreferredSize(new Dimension(MAP_WIDTH*TILE_WIDTH, MAP_HEIGHT*TILE_HEIGHT));
	}

	public Set<Tower> getTowers() {
		return towers;
	}
	
	private void setMap() {
		map = new ImageIcon[MAP_WIDTH][MAP_WIDTH];
		for (int i=0; i<map.length; i++) {
			for (int j=0; j<map[0].length; j++) {
				try {
					map[i][j] = new ImageIcon(new File("data/terrain/grass.png").toURI().toURL());
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void paint(Graphics g) {
		paintMap(g);
		paintTowers(g);
//		paintPath(g);
//		paintEnemies(g);
//		paintProjectiles(g);
	}
	
	private void paintMap(Graphics g) {
		for (int i=0; i<MAP_WIDTH; i++) {
			for (int j=0; j<MAP_HEIGHT; j++) {
				g.drawImage(map[i][j].getImage(), i*map[i][j].getIconWidth(), j*map[i][j].getIconHeight(), null);
			}
		}
	}
	
	private void paintTowers(Graphics g) {
		for (Tower t: towers) {
			Coord c = t.gridpos;
			g.drawImage(t.getTemplate().icon.getImage(), c.x-TILE_WIDTH/2, c.y-TILE_HEIGHT/2, null);
		}
	}

	@Override
	public void recieve(Netevent e) {
		if (e.cmd.equals("tower")) {
			String[] s = e.rest.split(" ");
			String name = s[0];
			int x = Integer.parseInt(s[1]);
			int y = Integer.parseInt(s[2]);
			Tower t = new Tower(towertemps.get(name));
			t.place(new Coord(x, y));
			towers.add(t);
			repaint();
		}
	}

/*	@Override
	public void update(Observable obs, Object dis) {
		TDMap tdmap = (TDMap)obs;
		Map<Coord, Tower> towers = tdmap.getTowerMap();
		for (Coord c: towers.keySet()) {
//			map[c.x][c.y] = towers.get(c).getTemplate().icon; 
		}
		
		List<Coord> tpath = tdmap.getPath();
		path = new ArrayList<Coord>();
		for (Coord c: tpath) {
			path.add(new Coord(c.x*TILE_WIDTH+TILE_WIDTH/2, c.y*TILE_HEIGHT+TILE_HEIGHT/2));
		}
		repaint();
	}*/
	/*	private void paintProjectiles(Graphics g) {
		Set<Projectile> proj = tdmap.getProjectiles();
		g.setColor(Color.CYAN);
		for (Projectile p: proj) {
			g.fillOval(p.getPos().x, p.getPos().y, 3, 3);
		}
	}*/
	/*	private void paintEnemies(Graphics g) {
		for (Enemy e: enemies) {
			ImageIcon icon = e.getTemplate().icon;
			g.drawImage(icon.getImage(), e.getPos().x-icon.getIconWidth()/2, e.getPos().y-icon.getIconHeight()/2, null);
		}
	}*/
	
	/*	private void paintPath(Graphics g) {
		if (path != null) {
			g.setColor(PATH_COLOR);
			for (int i=0; i<path.size()-1; ++i) {
				g.drawLine(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y);
			}
		}
	}*/
}
